package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

import mainController.ClientController;
import model.AnswerObject;
import model.Occurrence;

import view.ClientViewPanel;

public class ClientAppController {
	
	private ClientViewPanel panel;
	private ClientController clientController;
	
	public ClientAppController(ClientController clientController) {
		this.clientController = clientController;
		this.panel = new ClientViewPanel();
		
		this.panel.getjButtonSeeCar().addActionListener(new ListenerSeeCar());
	}
	
	public JPanel getPanel() {
		return this.panel;
	}	
	
	class ListenerSeeCar implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			String[] licensePlates = {"LCJ6514", "KLM3478", "KOA9823", "TEA9999", "KAH"};
			String licensePlate = licensePlates[new Random().nextInt(licensePlates.length)];
			ClientAppController.this.clientController.sendMessage(licensePlate);			
		}
	}

	public void show(AnswerObject message) {
		DefaultTableModel tableModel = (DefaultTableModel) this.panel.getjTable().getModel();

		for (Occurrence o : message.getOccurrence()) {			
			tableModel.insertRow(0, new String[] { message.getCar().getLicensePlate().getNumber().toString(), 
													message.getCar().getDriver().getName(), 
													o.getDate(),
													o.getPlace(),
													message.getFineNonPayed().isEmpty() ? "NÃO" : "SIM",
													message.getIpvaNonPayed().isEmpty() ? "NÃO" : "SIM",
													message.getCar().isStolen() ? "SIM" : "NÃO"});
		}
		this.panel.getjTable().setModel(tableModel);
		
		//System.out.println(message.getCar());
	}	
}
